//
//  ONSettingsViewController.m
//  Volatix
//
//  Created by Anh Tran on 6/5/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "ONSettingsViewController.h"

// Views
#import "ONSettingsTableViewCell.h"
#import "ONTabbarViewController.h"
#import "ONObjectAlert.h"
@interface ONSettingsViewController ()
@property (assign,nonatomic) int isTypeScreen;

@end

@implementation ONSettingsViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _isTypeScreen=[Utils getTypeScreen];
    _MainScreen=[Utils getMainScreen];
    
    
    
    if(_MainScreen)
    {
        /// will remove this code if
        _filter=[NSMutableArray arrayWithArray:[APP_DELEGATE getingFilterTimerForTypeScreen:_isTypeScreen]];
    }
    else
    {
        _filter=[NSMutableArray arrayWithArray:[APP_DELEGATE getingFilterBySymble:[self getRequestFiter:_isTypeScreen]]];
    }
    if(_filter ==nil)
    {
        _filter=[[NSMutableArray alloc] init];
    }
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //self.tabBarController.title = @"SYMBOL MANAGE";
    // (self.level==(LEVEL_ISO+1))?[NSString stringWithFormat:@"LOADZONE-%@",self.ISO]:(self.level==(LEVEL_LOADZONE+1))?[NSString stringWithFormat:@"POWERPOINT-%@",self.LOADZONE]:@"DAY AHEAD";
}
#pragma mark - UITableViewDataSource Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     return 50;//179;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSInteger numberOfCell = [self.dataArray count];
    return numberOfCell;
}
-(BOOL)checkExit:(NSString *)name andType:(int)type
{
    if(_isTypeScreen==3)//==> alertData
    {
        for(int index=0;index<[_filter count]; index++)
        {
            ONObjectAlert *_obj =[_filter objectAtIndex:index];
            if([name isEqualToString:_obj.symbleName]&&type==_obj.Type)
            {
                return true;
            }
        }
    }
    else{
        
    
        for(int index=0;index<[_filter count]; index++)
        {
            ONISOObjects *oNISOObject =[_filter objectAtIndex:index];
            if([name isEqualToString:oNISOObject.name])
            {
                return true;
            }
        }
    }
    return false;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *reuseIdentifier = @"ONSettingsTableViewCell";
    
    ONSettingsTableViewCell *cell = (ONSettingsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = (ONSettingsTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:reuseIdentifier owner:self options:nil] objectAtIndex:0];
    }
    
    cell.checkButton.userInteractionEnabled = NO;
    BOOL bChecking=false;
    if(_isTypeScreen==3)
    {
        ONObjectAlert *_obj = [self.dataArray objectAtIndex:indexPath.row];
        bChecking=[self checkExit:_obj.symbleName andType:_obj.Type];
        [cell loadCellDataAlert:_obj];
    }
    else
    {
        ONISOObjects *oNISOObject = [self.dataArray objectAtIndex:indexPath.row];
        bChecking=[self checkExit:oNISOObject.name andType:0];
        [cell loadCellData:oNISOObject];
    }
    cell.isSelected =bChecking;
    if (cell.isSelected) {
        [cell.checkButton setImage:[UIImage imageNamed:@"ic_checked_v1"] forState:UIControlStateNormal];
    } else {
        [cell.checkButton setImage:[UIImage imageNamed:@"ic_uncheck"] forState:UIControlStateNormal];
    }
    
    
    
    
    
    return cell;
}

#pragma mark - UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ONSettingsTableViewCell *cell = (ONSettingsTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    
   
    if (cell.isSelected) {
        cell.isSelected = NO;
        
        
        if(_isTypeScreen==3)
        {
            ONObjectAlert *oNISOObject = [self.dataArray objectAtIndex:indexPath.row];
            for(int index=0;index<[_filter count];index++)
            {
                ONObjectAlert *_obj =[_filter objectAtIndex:index];
                if([_obj.symbleName isEqualToString:oNISOObject.symbleName])
                {
                    [_filter removeObjectAtIndex:index];
                }
            }
            
        }
        else
        {
            
            ONISOObjects *oNISOObject = [self.dataArray objectAtIndex:indexPath.row];
            for(int index=0;index<[_filter count];index++)
            {
                ONISOObjects *_obj =[_filter objectAtIndex:index];
                if([_obj.name isEqualToString:oNISOObject.name])
                {
                    [_filter removeObjectAtIndex:index];
                }
            }
        }
        
        [cell.checkButton setImage:[UIImage imageNamed:@"ic_uncheck"] forState:UIControlStateNormal];
    } else {
         if(_isTypeScreen==3)
         {
             ONObjectAlert *oNISOObject = [self.dataArray objectAtIndex:indexPath.row];
             [_filter addObject:oNISOObject];
         }
         else
         {
            ONISOObjects *oNISOObject = [self.dataArray objectAtIndex:indexPath.row];
            [_filter addObject:oNISOObject];
         }
        cell.isSelected = YES;
        [cell.checkButton setImage:[UIImage imageNamed:@"ic_checked_v1"] forState:UIControlStateNormal];
    }
    
    // Deselect the row
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (IBAction)finishedButtonPressed:(id)sender {
    if(_MainScreen==true)
    {
        /// will remove this code if
        [APP_DELEGATE savingFilterTimer:_filter andTypeScreen:_isTypeScreen];
    }
    else
    {
        [APP_DELEGATE savingFilterBySymble:_filter andTypeScreen:[self getRequestFiter:_isTypeScreen]];
        //[APP_DELEGATE savingFilterBySymble:[NSString stringWithFormat:@"%@",@"AA"]];
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];

}

@end
